package com.echoesnet.echoesshortvideo.views.adapters;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.echoesnet.echoesshortvideo.AppController;
import com.echoesnet.echoesshortvideo.Persistence.VideoPathBean;
import com.echoesnet.echoesshortvideo.R;
import com.echoesnet.echoesshortvideo.Utils.Image.GlideApp;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Copyright (C) 2017 在线回声（天津）科技发展有限公司
 * 在线回声完全享有此软件的著作权，违者必究
 *
 * @author an
 * @version 1.0
 * @modifier
 * @createDate 2018/4/23 0023
 * @description
 */
public class VideoSelectAdapter extends RecyclerView.Adapter<VideoSelectAdapter.VideoSelectViewHolder>
{
    private Activity mAct;
    private List<VideoPathBean> videoList;
    private int itemWidth;
    private VideoSelectClick videoSelectClick;

    public void setVideoSelectClick(VideoSelectClick videoSelectClick)
    {
        this.videoSelectClick = videoSelectClick;
    }

    public VideoSelectAdapter(Activity mAct, List<VideoPathBean> videoList, int itemWidth)
    {
        this.mAct = mAct;
        this.videoList = videoList;
        this.itemWidth = itemWidth;
    }

    @NonNull
    @Override
    public VideoSelectViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
    {
        View itemView = LayoutInflater.from(mAct).inflate(R.layout.item_video_select,parent,false);
        return new VideoSelectViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull VideoSelectViewHolder holder, int position)
    {
        VideoPathBean  videoPathBean = videoList.get(position);
        if (videoPathBean != null)
        {
            GlideApp.with(AppController.getInstance())
                    .asBitmap()
                    .error(R.drawable.default_80px)
                    .placeholder(R.drawable.default_80px)
                    .load(videoPathBean.getVideoPath())
                    .centerCrop()
                    .into(holder.videoImg);
            holder.durationTv.setText(getTimes(videoPathBean.getDurantion()));
        }
        holder.itemView.setOnClickListener(view -> {
            if (videoSelectClick != null)
                videoSelectClick.itemClick(videoPathBean);
        });
    }

    @Override
    public int getItemCount()
    {
        return videoList.size();
    }
    private String getTimes(long durantion) {
        SimpleDateFormat format = new SimpleDateFormat("mm:ss");
        return format.format(new Date(durantion));
    }
    class VideoSelectViewHolder extends RecyclerView.ViewHolder{

        @BindView(R.id.img_video)
        ImageView videoImg;
        @BindView(R.id.tv_duration)
        TextView durationTv;

        public VideoSelectViewHolder(View itemView)
        {
            super(itemView);
            ButterKnife.bind(this,itemView);
            FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) videoImg.getLayoutParams();
            layoutParams.height = itemWidth;
            layoutParams.width = itemWidth;
            videoImg.setLayoutParams(layoutParams);
        }
    }

    public interface VideoSelectClick {
        void itemClick(VideoPathBean videoPathBean);
    }
}
